<!-- //登记弹窗 -->
<template>
	<view class="resgistration-container">

		<uni-forms ref="hiddenForm" :rules="rules" :modelValue="hiddenForm" border>
			<uni-forms-item label="隐患名称：" required name="hiddenDangerName">
				<uni-easyinput v-model="hiddenForm.hiddenDangerName" placeholder="请输入" :inputBorder="false" />
			</uni-forms-item>
			<uni-forms-item label="隐患等级：" required name="hiddenDangerLevel">
				<uni-data-select :localdata="hiddenLevelOption" v-model="hiddenForm.hiddenDangerLevel" />
			</uni-forms-item>
			<uni-forms-item label="隐患现场图片：" required name="taskName">
				<uni-file-picker v-model="hiddenFileInfoList" @select="handleSelect" @success="handleSuccess"
					@delete="handleDeleteImg" :imageStyles="imageStyles" file-mediatype="image"></uni-file-picker>
			</uni-forms-item>
			<!-- 上面选择检查计划回写上来的 -->
			<uni-forms-item label="隐患描述：" required name="hiddenDangerDescription">
				<uni-easyinput v-model="hiddenForm.hiddenDangerDescription" type="text" placeholder="请输入"
					:inputBorder="false" />

			</uni-forms-item>

			<uni-forms-item label="隐患责任单位：" required name="hiddenDangerDeptId">

				<custom-tree-select :listData="treeData" disabled dataLabel="label" dataValue="id" :search="true"
					:mutiple="false" v-model="hiddenForm.hiddenDangerDeptId" />

				</custom-tree-select>

			</uni-forms-item>

			<uni-forms-item label="检查执行人：" required name="hiddenDangerExaminerId">

				<uni-data-select :localdata="allUsers" disabled v-model="hiddenForm.hiddenDangerExaminerId"
					:arrayConfig="configParams" />
			</uni-forms-item>
			<!-- 多选 -->
			<uni-forms-item label="隐患登记人：" required name="hazardRegistrantName">

				<uni-easyinput v-model="hiddenForm.hazardRegistrantName" disabled focus
					placeholder="请输入内容"></uni-easyinput>
			</uni-forms-item>


			<uni-forms-item label="抄送到部门：" name="ccDepartmentId">
				<custom-tree-select :listData="treeData" dataLabel="label" dataValue="id" :search="true"
					:mutiple="false" v-model="hiddenForm.ccDepartmentId" />
				</custom-tree-select>
			</uni-forms-item>

			<uni-forms-item label="是否现场解决：" required name="isSiteSolution">
				<uni-data-checkbox v-model="hiddenForm.isSiteSolution" :localdata="solveOption"></uni-data-checkbox>
			</uni-forms-item>

			<!-- 更改区 -->
			<template v-if="hiddenForm.isSiteSolution == 1">
				<uni-forms-item label="整改情况" required prop="correctTheSituation">
					<uni-easyinput v-model="hiddenForm.correctTheSituation" type="text" placeholder="请输入"
						:inputBorder="false" />
				</uni-forms-item>
				<uni-forms-item label="隐患整改时间" required prop="rectificationTime">
					<uni-datetime-picker type="date" :clear-icon="false" v-model="hiddenForm.rectificationTime" />
				</uni-forms-item>
				<!-- 		<uni-forms-item label="隐患整改图片">
					<el-upload class="upload-demo" action="#" ref="upload2" :http-request="handleFileTwo"
						:on-remove="handleRemoveTwo" list-type="picture" multiple :limit="3" :file-list="fileListTwo">
						<el-button size="small" type="primary">点击上传</el-button>
					</el-upload>
					
					<uni-file-picker v-model="fileListTwo" @select="handleSelect" @success="handleSuccess"
						@delete="handleDeleteImg" :imageStyles="imageStyles" file-mediatype="image"></uni-file-picker>
				</uni-forms-item> -->
			</template>
			<!-- <kk></kk> -->
		</uni-forms>

		<view class="button">
			<button type="primary" size="mini" @click="submit">提交</button>
		</view>


	</view>

</template>

<script>
	import {
		mapGetters
	} from "vuex"
	import {
		deptTreeSelect
	} from "@/api/system/user";
	import {
		registerRegular,
		detailRegister,
		detailViewRegister
	} from "@/api/hiddenTrouble/periodShoot.js";
	export default {
		onLoad(e) {
			const eventChannel = this.getOpenerEventChannel();
			eventChannel.on('registrationAcct', (data) => {

				this.detailObj = data.detail,
					this.checkId = data.checkId,
					this.tableId = data.tableId
			})

			eventChannel.on('acceptDataFromOpenerPage', (res) => {
				console.log("获取修改数据", res)
				let {
					data
				} = res
				this.handleBcakToSummary(data)
			})
		},

		data() {
			return {
				//登记的ID
				tableId: null,
				//选中ID
				checkId: null,
				//详情
				detailObj: {},
				//校验规则
				rules: {},
				imageStyles: {
					width: 64,
					height: 64,
					border: {
						radius: ''
					}
				},
				//人员的格式化
				configParams: {
					valueKey: "userId",
					labelKey: "nickName"
				},
				//隐患现场图片
				hiddenFileInfoList: [],
				//部门数据
				treeData: [],
				//隐患登记
				hiddenForm: {
					isSiteSolution: 2,
					fileInfoList: []
				},
				hiddenLevelOption: [{
						text: '一般隐患',
						value: 1
					},
					{
						text: '重大隐患',
						value: 2,
						disable: true
					},

				],
				solveOption: [{
						text: '是',
						value: 1
					},
					{
						text: '否',
						value: 2
					},
				],
			}
		},
		methods: {
			//上传成功
			handleSuccess(val) {
				console.log("上传成功", val)
			},

			//删除
			handleDeleteImg(val) {
			
			},
			//选择文件
			handleSelect(val) {
				console.log("选择文件", val)
				let tempFiles = val.tempFiles[0]
				this.hiddenForm.fileInfoList.push({
					name: tempFiles.name,
					fileId: tempFiles.uuid,
					type: 25
				})
				console.log("文件list", this.hiddenForm)
			},


			//回写数据在汇总页面返回的
			async handleBcakToSummary(val) {
				let res = await detailRegister(val.id)
				// console.log("下半年", res)
				this.hiddenForm = res.data
				this.hiddenFileInfoList = res.data.fileInfoList
			},
			handleOpen(data) {
				let testObj = {}
				// console.log("登记", data, this.detailObj)
				// console.log("测试的", Reflect.ownKeys(testObj).length)
				this.hiddenForm.hiddenDangerDeptName = this.detailObj.passReviewUnitName;
				this.hiddenForm.hiddenDangerDeptId = this.detailObj.passReviewUnitId;
				this.hiddenForm.hiddenDangerExaminerId = this.detailObj.examineExecutorId;
				this.hiddenForm.hiddenDangerExaminerName = this.detailObj.examineExecutorName;
				this.hiddenForm.hazardRegistrantName = this.name;
			},
			/** 被检查部门部门下拉树结构 */
			async getDeptTree() {
				const response = await deptTreeSelect();
				this.treeData = response.data;
			},
			/** 提交**/
			submit() {
				this.$refs["hiddenForm"].validate().then(async (res) => {

					res.tableId = this.tableId;
					res.regularId = this.checkId ? this.checkId : this.hiddenForm.regularId;
					res.hiddenDangerDeptName = this.detailObj.passReviewUnitName
					res.hiddenDangerExaminerName = this.detailObj.examineExecutorName
					res.ccDepartmentId = res.ccDepartmentId ? res.ccDepartmentId.toString() : ""
					res.fileInfoList = this.hiddenForm.fileInfoList
					res.id = this.hiddenForm.id
					res.ccDepartmentId = this.hiddenForm.id ? this.hiddenForm.id : ""
					console.log("res===", res)
					uni.showLoading({
						title: "提交中"
					})
					await registerRegular(res)
					uni.hideLoading()
					uni.navigateBack()
				}).catch(err => {
					console.log('err', err);
				})

			},
		},
		mounted() {
			this.handleOpen()
			this.getDeptTree()

		},
		created() {

		},
		computed: {
			...mapGetters(["allUsers", "name"]),

		}
	}
</script>

<style lang="scss" scoped>
	.resgistration-container {
		padding: 20rpx;
		background-color: #fff;
		overflow-y: auto;
		height: 100vh;

		.button {
			text-align: right;
			margin: 14rpx;
		}

		::v-deep {
			.uni-forms-item--border {
				border-top: 0 solid #000;
				border-bottom: 2rpx solid #e8eaec;
			}

			.uni-forms-item__label {
				width: 40% !important;
			}

			.view-img {
				width: 200rpx;
				height: 200rpx;
			}

			.submit-btn {
				margin-top: 30rpx;
				background: #137ee9;
				border-radius: 10rpx;
			}
		}
	}
</style>